package course

import (
	"net/url"

	"net/http"

	"path/filepath"

	"gopkg.in/mgo.v2"
)

func Get(u *url.URL, w http.ResponseWriter, db *mgo.Database) {
	switch u.Query().Get("t") {
	case "s":
		query(filepath.Ext(u.Path), u.Query().Get("uid"), w, db)
	case "", "a":
		fallthrough
	default:
		all(filepath.Ext(u.Path), w, db)
	}
}

func Post(u *url.URL, w http.ResponseWriter, body []byte, db *mgo.Database) {
	create(filepath.Ext(u.Path), w, body, db)
}

func Put(u *url.URL, w http.ResponseWriter, body []byte, db *mgo.Database) {

	switch u.Query().Get("t") {
	case "", "i":
		fallthrough
	default:
		edit(filepath.Ext(u.Path), u.Query().Get("id"), w, body, db)
	}
}

func Del(u *url.URL, w http.ResponseWriter, db *mgo.Database) {
	switch u.Query().Get("t") {
	case "", "i":
		fallthrough
	default:
		remove(filepath.Ext(u.Path), u.Query().Get("id"), w, db)
	}
}
